home *** CD-ROM | disk | FTP | other *** search
/ Champak 50 / Volume 50 - JOGO DISK .iso / Games / moonstonemadness.swf / scripts / __Packages / SideScroller / BasicCamera.as next >
Text File  |  2007-09-27  |  5KB  |  155 lines

  1. class SideScroller.BasicCamera
  2. {
  3.    static var MODE_LOCK = 1;
  4.    static var MODE_FREE = 2;
  5.    static var BUFFER_SCREEN_LEFT = 50;
  6.    static var BUFFER_SCREEN_TOP = 50;
  7.    static var BUFFER_SCREEN_RIGHT = 50;
  8.    static var BUFFER_SCREEN_BOTTOM = 50;
  9.    static var BUFFER_FOLLOW = 4;
  10.    static var DEFAULT_FOLLOW_CENTER_X = 150;
  11.    static var DEFAULT_FOLLOW_CENTER_Y = 150;
  12.    static var SPEED_MAX = 25;
  13.    static var ACCELERATION = 30;
  14.    function BasicCamera(__nStageWidth, __nStageHeight, __oParent)
  15.    {
  16.       this.nStageWidth = __nStageWidth;
  17.       this.nStageHeight = __nStageHeight;
  18.       this.oParent = __oParent;
  19.       this.nMode = SideScroller.BasicCamera.MODE_FREE;
  20.       this.nFollowTargetPositionX = SideScroller.BasicCamera.DEFAULT_FOLLOW_CENTER_X;
  21.       this.nFollowTargetPositionY = SideScroller.BasicCamera.DEFAULT_FOLLOW_CENTER_Y;
  22.       this.nFollowAddonX = 0;
  23.       this.nFollowAddonY = 0;
  24.       this.nMaxSpeed = SideScroller.BasicCamera.SPEED_MAX;
  25.       this.nAcceleration = SideScroller.BasicCamera.ACCELERATION;
  26.       this.nCurrentX = 0;
  27.       this.nCurrentY = 0;
  28.       this.nSpeedX = 0;
  29.       this.nSpeedY = 0;
  30.       this.bFirstStartUp = true;
  31.       this.bPaused = false;
  32.       this.oParent.doAddListener(this);
  33.    }
  34.    function doPause()
  35.    {
  36.       this.bPaused = true;
  37.    }
  38.    function doResume()
  39.    {
  40.       this.bPaused = false;
  41.    }
  42.    function setFreeMode()
  43.    {
  44.       this.nMode = SideScroller.BasicCamera.MODE_FREE;
  45.       delete this.oLockedObject;
  46.    }
  47.    function doLockOn(__oElement)
  48.    {
  49.       this.nMode = SideScroller.BasicCamera.MODE_LOCK;
  50.       this.oLockedObject = __oElement;
  51.       if(this.bFirstStartUp)
  52.       {
  53.          this.bFirstStartUp = false;
  54.          this.doMoveTo(this.oLockedObject.__get__Ref()._x,this.oLockedObject.__get__Ref()._y);
  55.       }
  56.    }
  57.    function doEnterFrame()
  58.    {
  59.       if(!this.bPaused)
  60.       {
  61.          if(this.nMode == SideScroller.BasicCamera.MODE_LOCK)
  62.          {
  63.             this.nTargetX = this.oLockedObject.__get__Ref()._x - SideScroller.BasicCamera.BUFFER_SCREEN_LEFT - this.nFollowTargetPositionX - this.nFollowAddonX;
  64.             this.nTargetY = this.oLockedObject.__get__Ref()._y - SideScroller.BasicCamera.BUFFER_SCREEN_TOP - this.nFollowTargetPositionY - this.nFollowAddonY;
  65.          }
  66.          this.nSpeedX = (this.nTargetX - this.nCurrentX) / (100 / this.nAcceleration);
  67.          this.nSpeedY = (this.nTargetY - this.nCurrentY) / (100 / this.nAcceleration);
  68.          if(Math.abs(this.nSpeedX) > this.nMaxSpeed)
  69.          {
  70.             this.nSpeedX = this.nMaxSpeed * Library.Utils.MoreMath.getPolarity(this.nSpeedX);
  71.          }
  72.          if(Math.abs(this.nSpeedY) > this.nMaxSpeed)
  73.          {
  74.             this.nSpeedY = this.nMaxSpeed * Library.Utils.MoreMath.getPolarity(this.nSpeedY);
  75.          }
  76.          if(Math.abs(this.nSpeedX) < 0.2)
  77.          {
  78.             this.nSpeedX = 0;
  79.             this.nCurrentX = this.nTargetX;
  80.          }
  81.          if(Math.abs(this.nSpeedY) < 0.2)
  82.          {
  83.             this.nSpeedY = 0;
  84.             this.nCurrentY = this.nTargetY;
  85.          }
  86.          this.doMoveTo(this.nCurrentX + this.nSpeedX,this.nCurrentY + this.nSpeedY);
  87.       }
  88.    }
  89.    function doTravelTo(__nX, __nY)
  90.    {
  91.       this.setFreeMode();
  92.       this.nTargetX = __nX;
  93.       this.nTargetY = __nY;
  94.    }
  95.    function doMoveTo(__nX, __nY)
  96.    {
  97.       if(__nX < this.oParent.__get__Limits().left)
  98.       {
  99.          __nX = this.oParent.__get__Limits().left;
  100.       }
  101.       if(__nX > this.oParent.__get__Limits().right - this.nStageWidth)
  102.       {
  103.          __nX = this.oParent.__get__Limits().right - this.nStageWidth;
  104.       }
  105.       if(__nY < this.oParent.__get__Limits().top)
  106.       {
  107.          __nY = this.oParent.__get__Limits().top;
  108.       }
  109.       if(__nY > this.oParent.__get__Limits().bottom - this.nStageHeight)
  110.       {
  111.          __nY = this.oParent.__get__Limits().bottom - this.nStageHeight;
  112.       }
  113.       this.nCurrentX = __nX;
  114.       this.nCurrentY = __nY;
  115.    }
  116.    function doDestroy()
  117.    {
  118.       this.oParent.doRemoveListener(this);
  119.       delete this.oParent;
  120.       delete this.oLockedObject;
  121.    }
  122.    function get PosX()
  123.    {
  124.       return - this.nCurrentX;
  125.    }
  126.    function get PosY()
  127.    {
  128.       return - this.nCurrentY;
  129.    }
  130.    function set MaxSpeed(__nSpeed)
  131.    {
  132.       this.nMaxSpeed = __nSpeed;
  133.    }
  134.    function set Acceleration(__nAcceleration)
  135.    {
  136.       this.nAcceleration = __nAcceleration;
  137.    }
  138.    function set FollowAddonX(__nFollowAddonX)
  139.    {
  140.       this.nFollowAddonX = __nFollowAddonX;
  141.    }
  142.    function get FollowAddonX()
  143.    {
  144.       return this.nFollowAddonX;
  145.    }
  146.    function set FollowAddonY(__nFollowAddonY)
  147.    {
  148.       this.nFollowAddonY = __nFollowAddonY;
  149.    }
  150.    function get FollowAddonY()
  151.    {
  152.       return this.nFollowAddonY;
  153.    }
  154. }
  155.